package com.fashion.web.tag;

import com.fashion.model.News;
import com.fashion.service.NewsService;
import com.fashion.utils.NewsTemplate;
import com.fashion.utils.SpringBeanUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: keviyang
 * Date: 11-11-24
 */
public class ScriptTag extends SimpleTagSupport {
    public NewsService getNewsService() {
        return SpringBeanUtils.getBean("newsService", NewsService.class);
    }

    private int type; //标签类型
    private String columnName;  //新闻类型

    @Override
    public void doTag() throws JspException, IOException {
        try {
            NewsService newsService = getNewsService();
            JspWriter out = getJspContext().getOut();

            String text = generateHtmlByNews(newsService);

            out.print(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    protected String generateHtmlByNews(NewsService newsService) {
        List<NewsTemplate> templates = new ArrayList<NewsTemplate>();
        StringBuilder text = new StringBuilder("");
        StringBuilder pictureText = new StringBuilder("var pics='");
        StringBuilder linkText = new StringBuilder("var links='");
        StringBuilder titleText = new StringBuilder("var texts='");
        StringBuilder introductionText = new StringBuilder("var descripts='");
        List<News> newsList;

        /*
var pics="/images/index/01.gif###/images/index/02.gif";
var links="http://www.alixixi.com/###http://www.alixixi.com/";
var texts="CG渲染的街霸4春丽让人喷血###网游第一美女竟能秒杀周迅？###这不是CG！这是实际游戏截图";
var descripts="MーRS所制作的3D CG人物灵气不足，但风格强烈，让人###香港《明报》文娱版刊登了题为《李大齐与周迅分手的";
         */

        switch (type) {
            case 4:
                break;
            default:
                templates.add(new NewsTemplate(4, 6));

                newsList = newsService.searchNewsByColumnAndTemplateList(columnName, templates, 2);
                for (News inNews : newsList) {
                    pictureText.append("/thumbnail/").append(inNews.getBriefImage()).append("###");
                    linkText.append("/n-").append(inNews.getId()).append(".html###");
                    titleText.append(inNews.getTitle()).append("###");
                    introductionText.append(inNews.getIntroduction()).append("###");
                }

                pictureText.delete(pictureText.length() - 3, pictureText.length());
                linkText.delete(linkText.length() - 3, linkText.length());
                titleText.delete(titleText.length() - 3, titleText.length());
                introductionText.delete(introductionText.length() - 3, introductionText.length());

                pictureText.append("';");
                linkText.append("';");
                titleText.append("';");
                introductionText.append("';");
                text.append(pictureText).append("\n").append(linkText).append("\n").append(titleText).append("\n").append(introductionText).append("\n");
                break;
        }


        return text.toString();
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }
}
